package week10;

import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Work3
 * @description: TODO
 * @date 2021/11/11 14:10
 */
public class Work3 {
    public static void main(String[] args) {
        new MyFrame().Init();

    }
}

class MyFrame {
    JFrame frame = new JFrame();
    JLabel title = new JLabel("双色球开奖");
    JLabel red = new JLabel("红球");
    JLabel blue = new JLabel("蓝球");
    JTextField t1 = new JTextField(14);
    JTextField t2 = new JTextField(14);
    JButton b1 = new JButton("开始");
    JButton b2 = new JButton("停止");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    Boolean isStop = false;

    public void Init(){
        frame.setLayout(new GridLayout(4, 1));
        title.setFont(new Font("微软雅黑", Font.BOLD, 28));
        red.setFont(new Font("微软雅黑", Font.BOLD, 20));
        blue.setFont(new Font("微软雅黑", Font.BOLD, 20));
        t1.setFont(new Font("微软雅黑", Font.BOLD,18));
        t2.setFont(new Font("微软雅黑", Font.BOLD,18));

        p1.add(title);
        p2.add(red);
        p2.add(t1);
        p3.add(blue);
        p3.add(t2);
        p4.add(b1);
        p4.add(b2);

        frame.add(p1);
        frame.add(p2);
        frame.add(p3);
        frame.add(p4);

        frame.setVisible(true);
        frame.setSize(550, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isStop = true;
            }
        });

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isStop = false;
            }
        });

        class MyThread extends Thread {
            @Override
            public void run() {
                while (true) {
                    if (isStop) {
                        try {
                            sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        int r1 = (int) (Math.random()* 33 + 1);
                        int r2 = (int) (Math.random()* 33 + 1);
                        int r3 = (int) (Math.random()* 33 + 1);
                        int r4 = (int) (Math.random()* 33 + 1);
                        int r5 = (int) (Math.random()* 33 + 1);
                        int b = (int) (Math.random()* 16 + 1);

                        t1.setText(r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5);
                        t2.setText(b + "");
                    }
                }
            }
        }

        new MyThread().start();
    }
}


